import wxValidate from 'wechat-validate'
Page({
  behaviors: [wxValidate],
  data: {
    dateLayerVisible: false,
    houseLayerVisible: false,
    houseList: [],
    houseInfo: '',
    currentDate: Date.now(),
    maxDate: Date.now() + 1000 * 3600 * 24 * 3, // 最大时间
    //表单数据
    houseId: "",
    name: "",
    gender: 0,
    mobile: "",
    visitDate: ""
  },
  rules: {
    name: [
      { required: true, message: '访客姓名不能为空!' },
      { pattern: /[\u4e00-\u9fa5]{2,5}/, message: '访客姓名只能为中文!' },
    ],
    mobile: [
      { required: true, message: '访客手机号不能为空!' },
      { pattern: /^1[3-8]\d{9}$/, message: '请填写正确的手机号码!' },
    ],
    houseId: [
      { required: true, message: '请选择到访的房屋!' }
    ],
    visitDate: [
      { required: true, message: '请选择到访的日期!' }
    ],
  },
  onLoad() {
    this.getHouseList()
  },
  async getHouseList() {
    const res = await wx.http.get('/room')
    const houseList = res.map(item => ({
      name: item.point + item.building + item.room,
      id: item.id
    }))
    this.setData({
      houseList
    })
  },
  // 选择维修房屋
  selectHouse({ detail }) {
    console.log(detail);
    this.setData({
      houseId: detail.id,
      houseInfo: detail.name
    })
  },
  // 选择预约时间
  selectDateTime(e) {
    // console.log(e);
    this.setData(
      {
        dateLayerVisible: false,
        visitDate: wx.utils.dataFormat(e.detail)
      }
    )
  },
  //提交
  async goPassport() {
    const isPass = this.validate()
    if (!isPass) return
    const { name, gender, mobile, houseId, visitDate } = this.data
    const res = await wx.http.post('/visitor', { name, gender, mobile, houseId, visitDate })
    console.log(res);
    wx.utils.toast('上传成功')
    wx.reLaunch({
      url: '/visitor_pkg/pages/passport/index?id=' + res.id,
    })
  },
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },

})
